package untiring.controllers;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import untiring.interfaces.BoardService;
import untiring.interfaces.CommentService;
import untiring.models.BoardVO;
import untiring.models.CommentVO;

@Controller
@RequestMapping("/board/*")
public class BoardController {
	
	@Autowired
	private BoardService boardService;
	@Autowired
	private CommentService commentService;	
	
	@RequestMapping(value="/list", 
							method=RequestMethod.GET)
	public String list(Model model) throws Exception {
		
		model.addAttribute(
				"boards", boardService.readBoard() );
		
		return "board/list";
	}
	
	@RequestMapping(value="/write", 
			method=RequestMethod.GET)
	public String writeget() throws Exception {
		return "board/write";
	}
	
	@RequestMapping(value="/write", 
			method=RequestMethod.POST)
	public String writepost(
			@ModelAttribute BoardVO b
			) throws Exception {
		return boardService.newBoard( b );
	}
	
	@RequestMapping(value="/view", 
			method=RequestMethod.GET)
	public String view(Model model, 
		@RequestParam String bno) throws Exception {
		model.addAttribute("board", 
				boardService.readOneBoard(bno));
		
		model.addAttribute("comments", 
				commentService.readComment(bno) );
		return "board/view";
	}
	
	@RequestMapping(value="/view", 
			method=RequestMethod.POST)
	public String view(
			@ModelAttribute CommentVO c) throws Exception {
		//c.setBno( c.getBno().split(",")[0] );
		return commentService.newComment(c);
	}
	
	@RequestMapping(value="/update", 
			method=RequestMethod.GET)
	public String updateget() throws Exception {
		return "board/write";
	}
	@RequestMapping(value="/update", 
			method=RequestMethod.POST)
	public String updatepost() throws Exception {
		return "redirect: /board/list.mvc";
	}
	
	@RequestMapping(value="/deleteok", 
			method=RequestMethod.GET)
	public String delete() throws Exception {
		return "redirect: /board/list.mvc";
	}
	
	@RequestMapping(value="/reply", 
			method=RequestMethod.POST)
	public String reply(
			@ModelAttribute CommentVO c) throws Exception {
		return commentService.newReply(c);
	}
	
}





